{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "af6eaf0c",
   "metadata": {},
   "source": [
    "### datetime模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d92ad47e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 11, 10, 15, 40, 20, 543122)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "datetime.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5be2993c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用模块\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "df2ebcbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2021, 11, 10)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime 里面的 date 方法\n",
    "import datetime\n",
    "datetime.date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "852a3202",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime 里面的 date 方法 取出年月日\n",
    "import datetime\n",
    "datetime.date.today().year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a449bd68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import datetime\n",
    "datetime.date.today().month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1a7a5410",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import datetime\n",
    "datetime.date.today().day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "117c644c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 11, 10, 15, 41, 35, 595646)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime 里面的 datetime 方法 取出年月日时分秒\n",
    "datetime.datetime.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3ecf4486",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-11-10'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime 里面的 isoformat 方法 格式化时间\n",
    "datetime.date.isoformat(datetime.date.today())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8334702f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这一分钟是基数\n"
     ]
    }
   ],
   "source": [
    "# 从基数中选择时间节点\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"这一分钟是基数\")\n",
    "else:\n",
    "    print(\"这一分钟是偶数\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88c9a38c",
   "metadata": {},
   "source": [
    "## time模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f4e8cecd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入模块\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6939e385",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=7, tm_min=42, tm_sec=11, tm_wday=2, tm_yday=314, tm_isdst=0)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 国际标准时间\n",
    "time.gmtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3f4ab020",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=15, tm_min=42, tm_sec=16, tm_wday=2, tm_yday=314, tm_isdst=0)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本地时间\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b760d4c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'15:42'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 格式化时间\n",
    "time.strftime(\"%H:%M\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a3efd861",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-11-10'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# strftime 年月日\n",
    "time.strftime(\"%Y-%m-%d\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b73e047",
   "metadata": {},
   "source": [
    "## 循环与随机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1b4a7847",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入模块\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "04fa03de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3219765512882701"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机产生一个(0,1)之间的小数\n",
    "random.random()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4c9f8678",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机产生一个(1,10)之间的整数\n",
    "random.randint(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9d299fe2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这一分钟是基数\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "for i in range(5):\n",
    "    right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"这一分钟是基数\")\n",
    "else:\n",
    "    print(\"这一分钟是偶数\")\n",
    "    wait_time = random.randint(1,60)\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe260c20",
   "metadata": {},
   "source": [
    "## range的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0c7e6ef0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a7c95466",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(5, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6d6ec0c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 3, 6, 9]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(0, 10, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3f42d903",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-10, -40, -70]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(-10, -100, -30))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf221b1c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
